package com.tmt.im.wallet.controler;

import com.tmt.springboot.common.restful.ResultWrapper;
import com.tmt.im.common.enums.RedPackageType;
import com.tmt.im.common.pojo.RedPackageSkinPoJo;
import com.tmt.im.wallet.controler.vo.NewSkinRequest;
import com.tmt.im.wallet.controler.vo.UpdateSkinRequest;
import com.tmt.im.wallet.service.RedPackageSkinService;
import com.tmt.springboot.common.restful.IExecuteResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 红包皮肤控制器
 *
 * @Description TODO
 * @Author wuyi
 * @Date 2024/8/25 11:18
 * @Version 1.0
 **/
@Async("asyncTaskExecutor")
@Tag(name = "红包皮肤相关接口", description = "红包皮肤相关接口")
@RestController
@RequestMapping("${api-url-prefix}/skin")
public class RedPackageSkinController {

    private final RedPackageSkinService skinService;

    public RedPackageSkinController(RedPackageSkinService skinService) {
        this.skinService = skinService;
    }

    @Operation(summary = "获取红包皮肤图片接口",
            description = "获取红包皮肤图片接口",
            operationId = "getRedPackageSkin")
    @GetMapping(value = "/show/{skinId:.+}")
    public CompletableFuture<Void> getRedPackageSkin(@PathVariable("skinId") String strSkinId,
                                                     HttpServletResponse response, Principal principal) {
        String[] infos = strSkinId.split("\\.");
        Long skinId = Long.parseLong(infos[0]);
        String extendName = infos.length > 1 ? infos[1] : null;
        skinService.getSkin(skinId, extendName, response, principal);
        return CompletableFuture.completedFuture(null);
    }

    @Operation(summary = "新增红包皮肤",
            description = "新增红包皮肤",
            operationId = "addRedPackageSkin",
            parameters = {
                    @Parameter(name = "request", description = "新皮肤对象")
            })
    @PostMapping(value = "/redPackage/add")
    public CompletableFuture<IExecuteResult<RedPackageSkinPoJo>> addRedPackageSkin(NewSkinRequest request,
                                                                                   Principal principal) {
        IExecuteResult<RedPackageSkinPoJo> result = skinService.addSkin(request.getType(), request.getName(),
                request.getNewSkin(), principal);
        return CompletableFuture.completedFuture(result);
    }

    @Operation(summary = "获取指定类型的红包皮肤列表",
            description = "获取指定类型的红包皮肤列表",
            operationId = "getRedPackageSkinList",
            parameters = {
                    @Parameter(name = "type", description = "红包类型")
            })
    @GetMapping(value = "/redPackage/list/{type}")
    public CompletableFuture<IExecuteResult<List<RedPackageSkinPoJo>>> getRedPackageSkinList(@PathVariable("type") RedPackageType type,
                                                                                             Principal principal) {
        IExecuteResult<List<RedPackageSkinPoJo>> result = skinService.getSkinList(type, principal);
        return CompletableFuture.completedFuture(result);
    }

    @Operation(summary = "更新红包皮肤",
            description = "更新红包皮肤",
            operationId = "updateRedPackageSkin",
            parameters = {
                    @Parameter(name = "request", description = "更新皮肤对象")
            })
    @PostMapping(value = "/redPackage/update")
    public CompletableFuture<IExecuteResult<RedPackageSkinPoJo>> updateRedPackageSkin(@RequestBody UpdateSkinRequest request,
                                                                                      Principal principal) {
        IExecuteResult<RedPackageSkinPoJo> result = skinService.updateSkin(request, principal);
        return CompletableFuture.completedFuture(result);
    }

    @Operation(summary = "删除红包皮肤",
            description = "删除红包皮肤",
            operationId = "deleteRedPackageSkin",
            parameters = {
                    @Parameter(name = "skinId", description = "要删除的皮肤记录的ID")
            })
    @PostMapping(value = "/redPackage/delete/{skinId}")
    public CompletableFuture<IExecuteResult<ResultWrapper<Boolean>>> deleteRedPackageSkin(@PathVariable("skinId") Long skinId,
                                                                                         Principal principal) {
        IExecuteResult<Boolean> result = skinService.deleteSkin(skinId, principal);
        return CompletableFuture.completedFuture(ResultWrapper.from(result));
    }

}
